//
//  VC9Plist.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/28.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC9Plist: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        test4()
    }
    
    func test4() -> Void {
        let file = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        guard let filePath = file.first?.appending("/student.archiver") else {
            return
        }
        print("file=\(filePath)")
        
        // 数据
        let student = BStudent()
        student.age = 18
        student.name = "wanghzz"
        
        // 归档
        guard let archiver = try? NSKeyedArchiver.archivedData(withRootObject: student, requiringSecureCoding: true) else {
            print("111")
            return
        }
        try? archiver.write(to: URL(fileURLWithPath: filePath))
        
        // 解档
        guard let unarchiver = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archiver) else {
            return
        }
        print(unarchiver)
    }
    
    func test3() -> Void {
        let file = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        guard let filePath = file.first?.appending("/obj.archiver") else {
            return
        }
        print("file=\(filePath)")
        
        // 归档
        let archiver = NSKeyedArchiver(requiringSecureCoding: true)
        archiver.encode(18, forKey: "age")
        archiver.encode("wanghz", forKey: "name")
        archiver.finishEncoding()
        try? archiver.encodedData.write(to: URL(fileURLWithPath: filePath))
        
        // 解档
        let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: archiver.encodedData)
        guard let age = unarchiver?.decodeInt64(forKey: "age") else {
            return
        }
        guard let name = unarchiver?.decodeObject(forKey: "name") else {
            return
        }
        print("age=\(age) name=\(name)")
    }
    
    func test2() -> Void {
        let file = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        guard let filePath = file.first?.appending("/my.archiver") else {
            return
        }
        print("file=\(filePath)")
        
        // 归档
        guard let data = try? NSKeyedArchiver.archivedData(withRootObject: "123456", requiringSecureCoding: true) else {
            return
        }
        try? data.write(to: URL(fileURLWithPath: filePath))
        
        // 解档
        guard let result = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) else {
            return
        }
        print(result)
    }
    
    func test1() -> Void {
        let file = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        guard let filePath = file.first?.appending("/my.plist") else {
            return
        }
        print("file=\(filePath)")
        
        let dict:NSDictionary = ["aa":"bbbbbbbb"]
        dict.write(toFile: filePath, atomically: true)
        
        guard let dictt = NSDictionary(contentsOfFile: filePath) else {
            return
        }
        print("dict=\(dictt)")
    }
}
